/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package managed;

import java.io.Serializable;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.annotation.PostConstruct;
import javax.ejb.EJB;
import javax.enterprise.context.SessionScoped;
import javax.faces.context.ExternalContext;
import javax.faces.context.FacesContext;
import javax.inject.Named;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import session.AccountSessionBeanLocal;

/**
 *
 * @author antonio
 */
@Named(value = "accountController")
@SessionScoped
public class AccountControllerBean implements Serializable {
    
    private String password, rpassword, email;
    private String message = "";
    
    @EJB
    private AccountSessionBeanLocal accountBean;

    /**
     * Creates a new instance of AccountControllerBean
     */
    public AccountControllerBean() {
    }
    
    public String getUsername() {
        return this.accountBean.getUser().getUsername();
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    public String getRpassword() {
        return rpassword;
    }

    public void setRpassword(String rpassword) {
        this.rpassword = rpassword;
    }

    public String getEmail() {
        return email;
    }

    public void setEmail(String email) {
        this.email = email;
    }

    public String getMessage() {
        return message;
    }

    public void setMessage(String message) {
        this.message = message;
    }
    
    public void changePassword() {
        if(password == null || rpassword == null) {
            message = "Errore";
            return;
        }
        this.accountBean.changePassword(password);
    }
    
    public void changeEmail() {
        if(email == null) {
            message = "Errore";
            return;
        }
        this.accountBean.changeEmail(email);
    }
    
    public String logout() {
        this.accountBean.remove();
        FacesContext context = FacesContext.getCurrentInstance();
        HttpServletRequest request = (HttpServletRequest) context.getExternalContext().getRequest();
        request.getSession().invalidate();
        return "logout";
    }
    
    public String deleteAccount() { 
        this.accountBean.deleteAccount();
        return this.logout();
    }
    
    @PostConstruct
    private void init() {
        if(accountBean.getUser() == null) {
            try {
                getUserData();
            } catch (ServletException ex) {
                Logger.getLogger(GameSessionControllerBean.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
    }
    
    private void getUserData() throws ServletException {
        HttpServletRequest request = getRequest();
        accountBean.initUser(request.getRemoteUser());
    }
    
    private HttpServletRequest getRequest() throws ServletException {
        ExternalContext context = FacesContext.getCurrentInstance().getExternalContext();
        Object requestObject = context.getRequest();
        if(!(requestObject instanceof HttpServletRequest)) {
            Logger.getLogger(GameSessionControllerBean.class.getName()).log(Level.SEVERE, null, "Request object has type: " + requestObject.getClass());
            throw new ServletException();
        }
        HttpServletRequest request = (HttpServletRequest) requestObject;
        return request;
    }
    
}
